//
//  LeverObject.m
//  StarBugs
//
//  Created by ?? ? on 6/12/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Object/LeverObject.h"
#include "Util/ImageManager.h"

LeverObject::~LeverObject()
{
    
}

LeverObject::LeverObject(int type)
{
    mainSprite = new CCSprite;
    mainSprite->init();
    mainSprite->autorelease();
    addChild(mainSprite);
    
    if(type == objectType_leverBlue)
        lever = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "lever_blue");
    else
        lever = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "lever_red");
    
    lever->setAnchorPoint(ccp(0.5, 0));
    mainSprite->addChild(lever);
    
    CCSprite *sprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "lever_base");
    sprite->setAnchorPoint(ccp(0.5, 0.5));
    mainSprite->addChild(sprite);
    
    objectType = type;
    isEnable = true;
}

void LeverObject::setBody(b2World* _world)
{
    mainSprite->setPosition(position);
    mainSprite->setRotation(bodyAngle);
}

void LeverObject::resetObstacle()
{
    isEnable = true;
    lever->setRotation(0);
}

void LeverObject::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    
    mainSprite->setRotation(bodyAngle);
}

void LeverObject::setRun(int type)
{
    isEnable = false;
    CCRotateTo *rotate;
    if(type == 0)
        rotate = CCRotateTo::create(0.1, 80);
    else
        rotate = CCRotateTo::create(0.1, -80);
    lever->runAction(rotate);
}
